package com.yykj.hospital.server.service.impl;

import com.yykj.common.enums.ResultEnums;
import com.yykj.common.exception.NormalException;
import com.yykj.common.util.LocalObjectUtil;
import com.yykj.hospital.client.dto.DoctorDTO;
import com.yykj.hospital.server.form.DoctorDrugAdviceForm;
import com.yykj.hospital.server.model.entity.DoctorDrugAdvice;
import com.yykj.hospital.server.model.repository.DoctorDrugAdviceRepository;
import com.yykj.hospital.server.service.DrugAdviceService;
import com.yykj.hospital.server.service.HospitalDoctorService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class DrugAdviceServiceImpl implements DrugAdviceService {

    @Override
    public DoctorDrugAdvice editDoctorDrugAdvice(DoctorDrugAdviceForm form) {
        DoctorDrugAdvice doctorDrugAdvice;
        if(form.getId()!=null && form.getId() >0){
            doctorDrugAdvice = doctorDrugAdviceRepository.findById(form.getId()).orElseThrow(()->new NormalException(ResultEnums.NOT_EXIST));
            LocalObjectUtil.copyNullValue(form,doctorDrugAdvice);
        }else{
            doctorDrugAdvice = new DoctorDrugAdvice();
            BeanUtils.copyProperties(form,doctorDrugAdvice);
        }
        if(doctorDrugAdvice.getHospitalId() == null){
            DoctorDTO info = doctorService.info(form.getDoctorId());
            doctorDrugAdvice.setHospitalId(info.getHospitalId());
        }

        return doctorDrugAdviceRepository.save(doctorDrugAdvice);
    }

    @Override
    public List<DoctorDrugAdvice> listDoctorDrugAdvice(String doctorId) {
        return doctorDrugAdviceRepository.findAllByDoctorId(doctorId);
    }

    @Override
    public void deleteDoctorDrugAdvice(Integer id) {
        doctorDrugAdviceRepository.deleteById(id);
    }

    private DoctorDrugAdviceRepository doctorDrugAdviceRepository;

    private HospitalDoctorService doctorService;

    @Autowired
    public void setDoctorDrugAdviceRepository(DoctorDrugAdviceRepository doctorDrugAdviceRepository) {
        this.doctorDrugAdviceRepository = doctorDrugAdviceRepository;
    }
    @Autowired
    public void setDoctorService(HospitalDoctorService doctorService) {
        this.doctorService = doctorService;
    }
}
